<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #navselect .navselect_div {height: 39px;line-height: 39px;border-bottom: 1px solid #eee;}
    #navselect{border-right: 1px solid #eee;height: 100%;}
    #navselect #tree_content{font-size: 13px;overflow-x: auto;width: 100%;}
    #navselect #tree_content>div>div{display: grid !important;}
    #navselect .navselect_div{height: 35px;line-height:35px;border-bottom: 1px solid #eee;}
    #navselect .navselect_title{float: left;font-size: 14px;padding-left: 10px;}
    #navselect .navselect_nav{float: right;padding-right: 15px;}
    #navselect .navselect_nav a{font-size: 14px;cursor: pointer;}
    #checkallsub{width: 100%;}
    .listerror{margin-left: 10px!important;}
    #hejiinfo{padding-top: 10px;font-weight: bold;margin-left: 10px}
    #hejiinfo div{margin-right: 20px;float: left}
</style>
<body id="optPage" style="overflow-y: hidden">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">
        <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
            <ul class="layui-tab-title">
                <li class="layui-this">基本信息</li>
                <li>预算明细</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show" >
                    <div class="block-generic">
                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">申请人</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                                    </div>
                                </div>
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">申请部门</label>
                                    <div class="layui-input-block">
                                        <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                                    </div>
                                </div>
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="edittits"><span>基本信息</span></div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md12">
                                    <label class="layui-form-label"><text>*</text>项目名称</label>
                                    <div class="layui-input-block">
                                        <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid','80%','80%')">
                                        <div class="choose_div" id="enginid" key="1"></div>
                                        <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md6">
                                    <label class="layui-form-label"><text>*</text>预算主题</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">预算编号</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="number" id="number" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">编制人</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="bianer" id="bianer" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">编制日期</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="biandt" id="biandt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">预算金额(元)</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="money" id="money" placeholder="自动计算(根据明细)" disabled autocomplete="off" class="layui-input moneyinput disabled">
                                    </div>
                                </div>
                                <div class="layui-col-md6">
                                    <label class="layui-form-label">说明</label>
                                    <div class="layui-input-block radio-block">
                                        <input type="text" value="预算数据生成后，项目采购合同则必须基于预算" disabled autocomplete="off" class="layui-input disabled">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item height_auto">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md12">
                                    <label class="layui-form-label label_textarea">备注</label>
                                    <div class="layui-input-block">
                                        <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md12">
                                    <label class="layui-form-label">关联单据</label>
                                    <div class="layui-input-block">
                                        <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                                        <div class="choose_div" id="flowrelas" key="1"></div>
                                        <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide" id="advanersDiv">>
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md12">
                                    <label class="layui-form-label">预审人</label>
                                    <div class="layui-input-block relative">
                                        <div class="tanxian">
                                            <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                            <div class="tanxiannei" id="advaners"></div>
                                        </div>
                                        <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-form-item layui-hide" id="nexterDiv">
                            <div class="layui-row layui-col-space10">
                                <div class="layui-col-md12">
                                    <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                                    <div class="layui-input-block relative">
                                        <div class="tanxian">
                                            <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                            <div class="tanxiannei" id="nexter"></div>
                                        </div>
                                        <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <?php include '../public/upinfo.php'?>
                    </div>

                </div>
                <div class="layui-tab-item">
                    <div class="block-generic">
                        <div class="layui-form-item height_auto">
                            <div class="short_table_list" key="1">
                                <div class="layui-col-md2 laytp-container" id="navselect" style="background-color: #FFF!important;border: 1px solid #eee;">
                                    <div class="navselect_div">
                                        <div class="navselect_title">成本科目</div>
                                        <div class="navselect_nav">
                                            <a id="tree_pand" title="收起/展开">收起</i></a>&nbsp;
                                            <a id="tree_search" title="刷新"><i class="layui-icon layui-icon-refresh-1"></i></a>
                                        </div>
                                    </div>
                                    <div>
                                        <button id="checkallsub" type="button" class="layui-btn layui-btn-sm btn-dark">全部</button>
                                    </div>
                                    <div id="tree_content" style="padding-bottom: 20px;height: 600px"></div>
                                </div>
                                <div id="rightbox" class="layui-col-md10">
                                    <div class="opt-btn-group">
                                        <div style="margin-left: 10px"><span>明细：</span><span key="0" id="opname">全部</span></div>
                                        <div>
                                            <button type="button" id="importCtrlv" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn layui-hide">粘贴新增</button>
                                            <button type="button" id="importList" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                                        </div>
                                    </div>
                                    <table class="layui-table" lay-size="sm">
                                        <thead>
                                        <tr>
                                            <th width="45">序号</th>
                                            <th><text>*</text>科目</th>
                                            <th><text>*</text>名称及规格</th>
                                            <th><text>*</text>单位</th>
                                            <th><text>*</text>预算数量</th>
                                            <th><text>*</text>预算单价(元)</th>
                                            <th><text>*</text>预算金额(元)</th>
                                            <th>备注</th>
                                        </tr>
                                        </thead>
                                        <tbody class="short_table_tbody" id="budget_list"></tbody>
                                    </table>
                                    <div class="listerror">提示：<span></span></div>
                                    <div id="hejiinfo">
                                        <div>分项合计（元）</div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    var param_isturn = getQueryVariable('isturn') || 0;
    if(param_isturn > 0){
        $("#billzhan").remove();
        $("#billtutn").remove();
        $("#billsub").attr('isshen', 1);
    }
    var param_billyushen = getQueryVariable('billyushen') || 0;
    if(param_billyushen == 2){
        $("#billyushen").addClass('layui-hide');
    }

    $("#budget_list").css('max-height',document.documentElement.clientHeight-300);

    $("#commentForm").validate({
        rules: {
            name: {required: true},
        },
        messages:{
            name: {required: "请输入"},
        }
    });

    function choose_budsub(obj){
        var domkey = $(obj).parents('tr').index();
        openPage('../public/choose_engin_budsub.php?page=engin_budget&domkey='+domkey,'80%','80%');
    }

    var treeObj, deptTreeData=[], deptTreeStatus=1, navarr;
    layui.use(['laytp', 'form', 'tree'], function() {
        var form = layui.form, layer = layui.layer, tree = layui.tree;
        render_input_attr();

        getDeptData();
        $("#tree_search").click(function () {
            getDeptData();
        });
        $('#tree_pand').click(function () {
            if(deptTreeStatus == 1){
                treeObj.reload({id: 'deptTree',data: opt_tree(deptTreeData, false)});
                deptTreeStatus = 2;
                $("#tree_pand").html('展开');
            }else{
                treeObj.reload({id: 'tree_content',data: opt_tree(deptTreeData, true)});
                deptTreeStatus = 1;
                $("#tree_pand").html('收起');
            }
        });
        $('#checkallsub').click(function () {
            $("#opname").text('全部').attr('key',0);
            $("#importCtrlv").addClass('layui-hide');
            $("#importList").addClass('layui-hide');
            searchTableList();
            $('.layui-tree-entry').each(function () {
                $(this).removeClass('layui-tree-other');
            });
        });
        function getDeptData() {
            facade.ajax({url: ask_engin_budget_getbudsub}).done(function(res){
                if (res.code === 2000) {
                    deptTreeData = res.data;
                    deptTreeStatus = 1;
                    $("#tree_pand").html('收起');
                    treeObj = tree.render({
                        elem: '#tree_content'
                        ,id: 'tree_content'
                        ,data: deptTreeData
                        ,onlyIconControl: true
                        ,click: function(obj){
                            if(obj.data.children.length < 1){
                                $("#opname").text(obj.data.opname).attr('key',obj.data.id);
                                $("#importCtrlv").removeClass('layui-hide');
                                $("#importList").removeClass('layui-hide');
                                searchTableList();

                                $('.layui-tree-entry').each(function () {
                                    $(this).find('.layui-tree-txt').removeClass('layui-tree-other');
                                });
                                $(obj.elem).find('.layui-tree-entry').eq(0).find('.layui-tree-txt').addClass('layui-tree-other');
                            }
                        }
                    });
                }
            });
        }

        facade.ajax({type: 'get', url: ask_flowdata_edit_engin_budget, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                    if(data.advaners_info.length > 0){
                        tan_tags_init('#advaners', data.advaners_info);
                    }
                }
                if(data.isnext){
                    $("#nexterDiv").removeClass('layui-hide');
                    if(data.nexter_info.length > 0){
                        tan_tags_init('#nexter', data.nexter_info);
                    }
                }
                if(data.flowrelas_info.length > 0){
                    set_choose_btn_more('flowrelas',data.flowrelas_info);
                }


                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }



                $("#name").val(data.name);
                $("#number").val(data.number);
                $("#bianer").val(data.bianer);
                $("#biandt").val(data.biandt);
                $("#money").val(data.money);
                $("#explain").val(data.explain);


                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td>';
                listAddStr += '<input type="text" onclick="choose_budsub(this)" subid="0" readonly placeholder="请选择" class="layui-input subname">';
                listAddStr += '</td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="number" class="layui-input num"></td>';
                listAddStr += '<td><input type="number" class="layui-input price"></td>';
                listAddStr += '<td><input type="number" disabled class="layui-input money disabled"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr, 1);

                var list = res.data.list;
                if(list.length > 0){
                    var str = '';
                    for (var key in list){
                        var nums = parseInt(key)+1;
                        str += '<tr>';
                        str += '<td style="width: 45px">'+nums+'</td>';
                        str += '<td>';
                        str += '<input type="text" onclick="choose_budsub(this)" subid="'+list[key].subid+'" value="'+list[key].subname+'" readonly placeholder="请选择" class="layui-input subname">';
                        str += '</td>';
                        str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                        str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                        str += '<td><input type="text" class="layui-input num" value="'+list[key].num+'"></td>';
                        str += '<td><input type="text" class="layui-input price" value="'+list[key].price+'"></td>';
                        str += '<td><input type="text" disabled class="layui-input money disabled" value="'+list[key].money+'"></td>';
                        str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                        str += '</tr>';
                    }
                    $("#budget_list").html(str);
                }



                navarr = res.data.navarr;
                var navStr = '';
                for (var key in navarr){
                    navStr += '<div>'+navarr[key].name+'（<span key="'+navarr[key].id+'">0</span>）</div>';
                }
                navStr += '<div>总计（<span key="0">0</span>）</div>';
                $("#hejiinfo").append(navStr);
                searchTableList();

                public_upload_file_load('upload_files', res.data.files);
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.isshen = $(this).attr('isshen') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();

                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }



            $("#checkallsub").click();

            var list = [''];
            try {
                $('#budget_list tr').each(function (i) {
                    var subid = $(this).find('.subname').attr('subid');
                    var name = $(this).find('.name').val();
                    var unit = $(this).find('.unit').val();
                    var num = $(this).find('.num').val();
                    var price = $(this).find('.price').val();
                    var money = $(this).find('.money').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!subid && !name && !unit && num=='' && price=='' && !money && !explain){
                        return false;
                    }
                    if(data.field.isturn > 0){
                        if(!subid){
                            throw new Error('第'+index+'行，请选择科目');
                        }
                        if(!name){
                            throw new Error('第'+index+'行，请输入名称及规格');
                        }
                        if(!unit){
                            throw new Error('第'+index+'行，请输入单位');
                        }
                        if(!num){
                            throw new Error('第'+index+'行，请输入预算数量');
                        }
                        if(!price){
                            throw new Error('第'+index+'行，请输入预算单价');
                        }

                        var chongKey = check_chongfu(i, subid, name, unit);
                        if(chongKey){
                            throw new Error('第'+chongKey+'行'+'与第'+index+'行，名称及规格重复');
                        }
                    }

                    list.push({
                        subid:subid,
                        name:name,
                        unit:unit,
                        num:num,
                        price:price,
                        money:money,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            public_flow_submit(data.field, ask_flowdata_add_engin_budget, ask_flowdata_edit_engin_budget, $(this).attr('but'));
            return false;
        });

    });


    //检测重复
    function check_chongfu(index, subid, name, unit){
        try {
            $('#budget_list tr').each(function (i) {
                var cur_subid = $(this).find('.subname').attr('subid');
                var cur_name = $(this).find('.name').val();
                var cur_unit = $(this).find('.unit').val();
                if(index != i){
                    if(subid === cur_subid && name === cur_name && unit === cur_unit){
                        throw new Error(i+1);
                    }
                }
            });
        } catch(e) {
            return e.message;
        }

        return 0;
    }




    $('#budget_list').on('input propertychange','.num,.price',function () {
        auto_jisuan_money();
    });

    //自动处理预算金额
    function auto_jisuan_money() {
        var hejiarr = [];
        var alllmoney = 0;
        $('#budget_list tr').each(function (i) {
            var num = parseValidNum($(this).find('.num').val(),3);
            var price = parseValidNum($(this).find('.price').val());
            var money = (num * price).toFixed(2);

            // $(this).find('.num').val(num);
            // $(this).find('.price').val(price);
            $(this).find('.money').val(money);

            var subid = $(this).find('.subname').attr('subid') || 0;
            if(subid > 0){
                var __subid = get_navarr_id(subid);
                if(!hejiarr[__subid]){
                    hejiarr[__subid] = parseFloat(money);
                }else{
                    hejiarr[__subid] += parseFloat(money);
                }
            }
            alllmoney = parseFloat(alllmoney)+parseFloat(money);
        });

        $("#hejiinfo").find('span').text(0);
        for (var iii in hejiarr){
            $("#hejiinfo").find('span[key="'+iii+'"]').text(parseFloat(hejiarr[iii]).toFixed(2));
        }
        $("#hejiinfo").find('span[key="0"]').text(alllmoney.toFixed(2));
        $("#money").val(alllmoney.toFixed(2));

    }


    //更新明细表格
    function searchTableList() {
        update_refresh_row_func();
        var opkey = $("#opname").attr('key');
        if(opkey < 1){
            $('#budget_list tr').removeClass('layui-hide');
        }else{
            $('#budget_list tr').each(function (i) {
                var subid = $(this).find('.subname').attr('subid');
                if(!subid){
                    $(this).addClass('layui-hide');
                }else{
                    if(opkey == subid){
                        $(this).removeClass('layui-hide');
                    }else{
                        $(this).addClass('layui-hide');
                    }
                }
            });
        }

        var hangKey=1;
        $('#budget_list tr').each(function (i) {
            if($(this).hasClass('layui-hide')){
                $(this).children('td:first-child').html(0);
            }else{
                $(this).children('td:first-child').html(hangKey);
                hangKey++;
            }
        });

        auto_jisuan_money();
    }
    //重置方法
    function short_table_opt_func(bridge_str, table_key, xmObj={}){
        if(!table_key || table_key == undefined || table_key == 'undefined'){
            table_key = 1;
        }
        update_refresh_row_func();

        $(".short_table_add_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }

            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            var opkey = $("#opname").attr('key');
            var opname = $("#opname").text();
            if(!sle){
                $(this).parents('.short_table_list').find('table').children('tbody').append(bridge_str);
                if(opkey > 0){
                    var hangObjs = $('#budget_list tr');
                    $(hangObjs[hangObjs.length-1]).find('.subname').val(opname).attr('subid',opkey);
                }
            }else{
                $(sle_obj).after(bridge_str);
                if(opkey > 0){
                    var hangObjs = $('#budget_list tr');
                    $(hangObjs[$(sle_obj).index()+1]).find('.subname').val(opname).attr('subid',opkey);
                }
            }

            var hangKey = 1;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                if($(this).hasClass('layui-hide')){
                    $(this).children('td:first-child').html(0);
                }else{
                    $(this).children('td:first-child').html(hangKey);
                    hangKey++;
                }
            });

            searchTableList();

            // 设置滚动条
            if($(this).parents('.short_table_list').find('table').children('tbody').height() >= 200){
                $(this).parents('.short_table_list').find('table').children('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
        });

        $(".short_table_copy_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj,sle_html;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                    sle_html = '<tr>'+$(this).formhtml()+'</tr>';

                    if($(this).find('.filedetaobjdata').html() != undefined){
                        var filedetaobjdataStr = $(this).find('.filedetaobjdata').html();
                        var filedetaobjdataLength = $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html();
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(0);
                        }
                        $(this).find('.filedetaobjdata').html('[]');
                        sle_html = '<tr>'+$(this).formhtml()+'</tr>';
                        if(filedetaobjdataLength > 0){
                            $(this).find('.filedetaobjdata').parent('div').find('span:first-child').html(filedetaobjdataLength);
                        }
                        $(this).find('.filedetaobjdata').html(filedetaobjdataStr);
                    }
                }
            });

            if(!sle){
                layer.msg("请选中要复制的行");return false;
            }else{
                $(sle_obj).after(sle_html);
            }

            var hangKey = 1;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                if($(this).hasClass('layui-hide')){
                    $(this).children('td:first-child').html(0);
                }else{
                    $(this).children('td:first-child').html(hangKey);
                    hangKey++;
                }
            });
            searchTableList();

            // 设置滚动条
            if($(this).parents('.short_table_list').find('tbody').height() >= 200){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','scroll')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
        });

        $(".short_table_del_row").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }
            var sle = false;
            var sle_obj;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function () {
                // 样式处理
                $(this).find('td').css({'border': '1px solid #eee'});
                $('.layui-table thead th ').css({'border-bottom-color': '#eee'});

                if($(this).attr('sle') == 'sle'){
                    sle = true;
                    sle_obj = $(this);
                }
            });

            if(!sle){
                layer.msg("请选中要删除的行");return false;
            }else{
                $(sle_obj).remove();
            }

            // if($(this).parents('.short_table_list').find('table').children('tbody').children('tr').length < 1){
            //     $(this).parents('.short_table_list').children('table').children('tbody').append(bridge_str);
            // }


            var hangKey = 1;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                if($(this).hasClass('layui-hide')){
                    $(this).children('td:first-child').html(0);
                }else{
                    $(this).children('td:first-child').html(hangKey);
                    hangKey++;
                }
            });

            searchTableList();

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 200){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
        });

        $(".short_table_del_all").click(function () {
            if($(this).parents('.short_table_list').attr('key') != table_key){
                return false;
            }

            var opkey = $("#opname").attr('key');
            if(opkey > 0){
                $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                    var subid = $(this).find('.subname').attr('subid') || 0;
                    if(opkey == subid){
                        $(this).remove();
                    }
                });
            }else{
                $(this).parents('.short_table_list').find('table').children('tbody').html('');
            }

            var hangKey = 1;
            $.each($(this).parents('.short_table_list').find('table').children('tbody').children('tr'),function (key,val) {
                if($(this).hasClass('layui-hide')){
                    $(this).children('td:first-child').html(0);
                }else{
                    $(this).children('td:first-child').html(hangKey);
                    hangKey++;
                }
            });
            searchTableList();

            // 取消滚动条
            if($(this).parents('.short_table_list').find('tbody').height() < 200){
                $(this).parents('.short_table_list').find('thead').css('overflow-y','hidden')
            }

            render_date();
            render_from();
            update_refresh_row_func();
            set_list_render_xmselect(xmObj);
        });
    }

    //导入
    function import_list() {
        var opkey = $("#opname").attr('key');
        if(!opkey || opkey < 1){
            facade.error("请选择要导入的成本科目");
            return false;
        }
        openPage('../public/import.php?reveal=engin_budget@importdata&down='+encodeURI('成本预算'), '770px', '490px');
    }

    function opt_tree(data, status) {
        for (var i=0;i<data.length;i++){
            data[i]['spread'] = status;
            if(data[i].children != undefined){
                opt_tree(data[i].children, status);
            }
        }
        return data;
    }


    function get_navarr_id(subid){
        var fid = 0;
        for (var key in navarr){
            if(in_array(subid, navarr[key].children)){
                fid = navarr[key].id;
            }
        }
        return fid;
    }

    function update_refresh_row_func() {

        $.each($("#budget_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }









    var ctrlv_field_name = [
        {field: 'name', name:'名称及规格'},
        {field: 'unit', name:'单位'},
        {field: 'num', name:'预算数量',type:'number'},
        {field: 'price', name:'预算单价',type:'number'},
        {field: 'explain', name:'备注'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){

            var opkey = $("#opname").attr('key');
            var opname = $("#opname").text();
            if(!opkey || opkey < 1){
                opname = '';
            }

            var nums = $("#budget_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td>';
                str += '<input type="text" onclick="choose_budsub(this)" value="'+opname+'" subid="'+opkey+'" readonly placeholder="请选择" class="layui-input subname">';
                str += '</td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                str += '<td><input type="number" class="layui-input num" value="'+list[key].num+'"></td>';
                str += '<td><input type="number" class="layui-input price" value="'+list[key].price+'"></td>';
                str += '<td><input type="number" class="layui-input money disabled" disabled></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#budget_list").append(str);
            }
        }
        searchTableList();
        render_date();
        render_from();
    }
</script>
</body>
</html>
